<template>
  <div class="big-screen">
    <!-- 顶部图片 -->
    <div class="home-top"></div>
    <!-- 底部图片 -->
    <div class="home-bottom" @click="goHome"></div>
    <!-- 中间的内容 -->
    <div class="content_left">
      <div class="content_left_top">
        <div class="patient-risk-title">
          <div class="blue_block"></div><div>患者危险性占比</div></div
        >
        <div class="progress">
          <div class="progress-item">
            <div class="progress-title">
              <div class="crown1"></div>
              <div class="progress-description">
                {{ riskProportion.firstHospitalName }}
              </div>
            </div>
            <div class="progress-bar">
              <div class="progress-fill">
                <PercentBar
                  :value="riskProportion.firstCount"
                  :max="total"
                  :bar-background="'linear-gradient(to right, #00318A 0%, #37FFB8 100%)'"
                />
              </div>
              <div class="progress-number">{{ riskProportion.firstCount }}人</div>
            </div>
          </div>
          <div class="progress-item">
            <div class="progress-title">
              <div class="crown2"></div>
              <div class="progress-description">
                {{ riskProportion.secondHospitalName }}
              </div>
            </div>
            <div class="progress-bar">
              <div class="progress-fill">
                <PercentBar
                  :value="riskProportion.secondCount"
                  :max="total"
                  :bar-background="'linear-gradient(to right, #00318A 0%, #37FFB8 100%)'"
                />
              </div>
              <div class="progress-number">{{ riskProportion.secondCount }}人</div>
            </div>
          </div>
          <div class="progress-item">
            <div class="progress-title">
              <div class="crown3"></div>
              <div class="progress-description">
                {{ riskProportion.thirdHospitalName }}
              </div>
            </div>
            <div class="progress-bar">
              <!-- 进度条 -->
              <div class="progress-fill">
                <PercentBar
                  :value="riskProportion.thirdCount"
                  :max="total"
                  :bar-background="'linear-gradient(to right, #00318A 0%, #37FFB8 100%)'"
                />
              </div>
              <div class="progress-number">{{ riskProportion.thirdCount }}人</div>
            </div>
          </div>
        </div>
        <div class="pie-bg"></div>
        <div id="pie-chart" class="pie-chart"></div>
      </div>
      <div class="content_left_bottom">
        <div class="patient-risk-title">
          <div class="blue_block"></div>
          <div>精神病患者新增趋势</div>
        </div>
        <div id="recent-three-add-chart" class="recent-three-add-chart"></div>
      </div>
    </div>
    <div class="content_middle">
      <div class="patient-alarm-title">
        <div class="patient-alarm-title-right">
          <div class="blue_block"></div>
          <div>精神病人定时报警系统 ({{ alarmTotal }}人)</div>
        </div>
        <div class="send-all-email">一键发送</div>
      </div>

      <ul v-infinite-scroll="alarmLoad" class="alarm-content">
        <li v-for="(item, index) in alarmItems" :key="index" class="alarm-content-item">
          <el-tooltip placement="top">
            <template #content> {{ item.patientName }}</template>
            <div class="alarm-name">姓名:{{ item.patientName }}</div>
          </el-tooltip>
          <el-tooltip placement="top">
            <template #content> {{ item.deptName }}</template>
            <div class="alarm-hospital">医院:{{ item.deptName }}</div>
          </el-tooltip>
          <el-tooltip placement="top">
            <template #content> {{ item.idNumber }}</template>
            <div class="alarm-idcard">身份证号:{{ item.idNumber }}</div>
          </el-tooltip>
          <el-tooltip placement="top">
            <template #content> {{ formatDate(item.lastFollowUpDate, 'YYYY-MM-DD') }}</template>
            <div class="alarm-time"
              >上次随访时间:{{ formatDate(item.lastFollowUpDate, 'YYYY-MM-DD') }}</div
            >
          </el-tooltip>
          <div class="send-email" @click="sendEmail(item.id)"> 发送短信提醒 </div></li
        >
      </ul>
    </div>
    <div class="content_right">
      <div class="content_right_top">
        <div class="patient-risk-title">
          <div class="blue_block"></div>
          <div>高风险患者新增趋势</div>
        </div>
        <div id="hight-risk-chart" class="hight-risk-chart"></div>
      </div>
      <div class="content_right_middle">
        <div class="patient-risk-title">
          <div class="blue_block"></div>
          <div>随访趋势</div>
        </div>
        <div id="random-chart" class="random-chart"></div>
      </div>
      <div class="content_right_bottom">
        <div class="patient-risk-title">
          <div class="blue_block"></div>
          <div>治愈率榜单</div>
        </div>
        <div class="cure-rate-list">
          <div class="cure-top3">
            <div class="top-content">
              <div class="cure-blue-bg"
                ><div class="top-title" style="color: #40a9ff">NO.2</div>
                <div class="top2-icon"></div>
              </div>
              <el-tooltip placement="top">
                <template #content> {{ cureTop.secoundName }}</template>
                <div class="hospital-name">{{ cureTop.secoundName }}</div>
              </el-tooltip>
            </div>
            <div class="top-content">
              <div class="cure-orange-bg"
                ><div class="top-title" style="color: #fdcc56">NO.1</div>
                <div class="top1-icon"></div>
              </div>
              <el-tooltip placement="top">
                <template #content> {{ cureTop.firstName }}</template>
                <div class="hospital-name">{{ cureTop.firstName }}</div>
              </el-tooltip>
            </div>
            <div class="top-content">
              <div class="cure-blue-bg">
                <div class="top-title" style="color: #40a9ff">NO.3</div>
                <div class="top3-icon"></div>
              </div>
              <el-tooltip placement="top">
                <template #content> {{ cureTop.thirdName }}</template>
                <div class="hospital-name">{{ cureTop.thirdName }}</div>
              </el-tooltip>
            </div>
          </div>
          <div class="cure-other">
            <ul v-infinite-scroll="load" class="infinite-list" style="overflow: auto">
              <li
                v-for="(item, index) in cureLists"
                :key="index"
                :class="'infinite-list-item' + ((index + 1) % 2)"
                ><div class="horn"></div>
                <div class="other-content"
                  >{{ item.managementOrgName }}：{{ item.numberOfPeople }}人</div
                ></li
              >
            </ul>
            <!-- <div
              :class="'cure-item' + ((index + 1) % 2)"
              v-for="(item, index) in cureLists"
              :key="index"
            >
              <div class="horn"></div>
              <div class="other-content">{{ item.name }}：{{ item.number }}人</div>
            </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {
  getPie3D,
  getBarChartOption,
  getBarAndLineChartOption,
  getLineChartOption
} from './3DCharts'
import * as BigScreenApi from '@/api/patient/bigScreen'
import { formatDate } from '@/utils/formatTime'
import { da } from 'element-plus/es/locale'
defineOptions({ name: 'BigScreen' })

const echarts = getCurrentInstance()!.appContext.config.globalProperties.$echarts

const queryParams = ref({
  pageNo: 1,
  pageSize: 10
})

const alarmQueryParams = ref({
  pageNo: 1,
  pageSize: 20
})

const cureTotal = ref(0)
const cureLists = ref([])
const cureTop = ref({})
const getCureList = async () => {
  const data = await BigScreenApi.getCureRate(queryParams)
  //只取前三的医院
  if (queryParams.value.pageNo === 1) {
    // 取data前三个的managementOrgName分别赋值给cureTop的firstName、secoundName、thirdName
    cureTop.value.firstName = data.list[0]?.managementOrgName || ''
    cureTop.value.secoundName = data.list[1]?.managementOrgName || ''
    cureTop.value.thirdName = data.list[2]?.managementOrgName || ''
  }
  cureTotal.value = data.total
  // 将data.list追加到cureLists，保证顺序
  cureLists.value = [...cureLists.value, ...data.list]
}

const alarmTotal = ref(0)
const alarmItems = ref([])
const getAlarmList = async () => {
  const data = await BigScreenApi.getAlarm(alarmQueryParams.value)
  alarmTotal.value = data.total
  alarmItems.value = [...alarmItems.value, ...data.list]
}

const riskProportion = ref({})
const total = ref(100)
const getProportion = async () => {
  const data = await BigScreenApi.getRiskProportion()
  total.value = data.firstCount + data.secondCount + data.thirdCount
  riskProportion.value = data
}

const initPie3D = async () => {
  const data = await BigScreenApi.getRiskTypeProportion()
  const pieData = []
  data?.forEach((element) => {
    pieData.push({
      name: element.riskTypeName,
      value: element.riskTypeCount
    })
  })
  const pieChart = document.getElementById('pie-chart')
  if (pieChart) {
    const option = getPie3D(pieData, 0)
    const pieChartInstance = echarts.init(pieChart)
    pieChartInstance.setOption(option)
    window.addEventListener('resize', () => {
      pieChartInstance.resize()
    })
  }
}

const initBar3D = async () => {
  const data = await BigScreenApi.getIncreaseProportion()
  const barData = [
    {
      name: '新增',
      data: []
    }
  ]
  data?.forEach((element) => {
    barData[0].data.push({
      name: element.increaseTime,
      value: element.increaseCount
    })
  })
  const barChart = document.getElementById('recent-three-add-chart')
  if (barChart) {
    const option = getBarChartOption(barData)
    const barChartInstance = echarts.init(barChart)
    barChartInstance.setOption(option)
    window.addEventListener('resize', () => {
      barChartInstance.resize()
    })
  }
}

const initBarAndLine3D = async () => {
  const data = await BigScreenApi.getFollowUpTrends()
  const xLabel = []
  const lineData = []
  const barData = []
  data?.forEach((element) => {
    xLabel.push(element.month)
    lineData.push(element.finishCount)
    barData.push(element.unFinishCount)
  })
  const barAndLineChart = document.getElementById('random-chart')
  if (barAndLineChart) {
    const option = getBarAndLineChartOption(barData, lineData, xLabel)
    const barAndLineChartInstance = echarts.init(barAndLineChart)
    barAndLineChartInstance.setOption(option)
    window.addEventListener('resize', () => {
      barAndLineChartInstance.resize()
    })
  }
}

const initLine3D = async () => {
  const xLabel = []
  const lineData = []
  const data = await BigScreenApi.getHighRiskPatients()
  data?.forEach((element) => {
    xLabel.push(element.month)
    lineData.push(element.count)
  })
  const lineChart = document.getElementById('hight-risk-chart')
  if (lineChart) {
    const option = getLineChartOption(xLabel, lineData, '人数')
    const lineChartInstance = echarts.init(lineChart)
    lineChartInstance.setOption(option)
    window.addEventListener('resize', () => {
      lineChartInstance.resize()
    })
  }
}
const goHome = () => {
  // 新页面打开大屏可视化
  window.open('/baseInfo', '_blank')
}

//无限滚动接口
const load = () => {
  // 判断是否还有更多数据需要加载
  if (queryParams.value.pageNo * queryParams.value.pageSize < cureTotal.value) {
    // 当前页数加1，调getCureList接口
    queryParams.value.pageNo += 1
    getCureList()
  }
}

//无限滚动接口
const alarmLoad = () => {
  // 判断是否还有更多数据需要加载
  if (alarmQueryParams.value.pageNo * alarmQueryParams.value.pageSize < alarmTotal.value) {
    // 当前页数加1，调getCureList接口
    alarmQueryParams.value.pageNo += 1
    getAlarmList()
  }
}
//挂载时候加载
onMounted(() => {
  getProportion()
  getAlarmList()
  getCureList()
  initPie3D()
  initBar3D()
  initBarAndLine3D()
  initLine3D()
})
</script>
<style lang="scss" scoped>
.big-screen {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-image: url('@/assets/patient/home/home-bg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  //加一个蒙版层
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
}
.home-top {
  width: 100%;
  height: 119px;
  position: absolute;
  top: 0;
  left: 0;
  background-image: url('@/assets/patient/home/home-top.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  //在中间添加文字"数据分析"
  &::after {
    content: '驻马店市严重精神障碍患者服务管理系统';
    position: absolute;
    top: 60%;
    left: 50%;
    transform: translate(-55%, -50%);
    font-size: 22px;
    color: #fff;
    font-weight: bold;
  }
}
.home-bottom {
  width: 1063px;
  height: 71px;
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  background-image: url('@/assets/patient/home/home-bottom.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  cursor: pointer;
  //在中间添加文字
  &::after {
    content: '主页';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    color: #ffffff88;
    font-weight: bold;
  }
  //移入动画
  &:hover {
    //放大
    transform: translateX(-50%) scale(1.05);
    //平滑过渡
    transition: transform 0.3s ease-in-out;
  }
}
.content_left {
  width: 463px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
  .content_left_top {
    width: 100%;
    height: 410px;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    background-image: url('@/assets/patient/home/content-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    .progress {
      width: 100%;
      height: 140px;
      padding: 10px 20px;
      margin: 5px 0 5px 0;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: center;
    }
    .pie-chart {
      width: 100%;
      height: 225px;
      background-image: url('@/assets/patient/home/pieChart-bg.png');
      background-size: 80% 100%;
      background-repeat: no-repeat;
      background-position: center;
      //添加一个透明高亮背景
      // &::before {
      //   content: '';
      //   position: absolute;
      //   top: 0;
      //   left: 32%;
      //   width: 171px;
      //   height: 148px;
      //   //从上到下从透明渐变成#D8DFF1
      //   background: linear-gradient(to bottom, transparent 0%, #083588 100%);
      // }
    }
  }
  .content_left_bottom {
    width: 100%;
    height: 395px;
    position: relative;
    background-image: url('@/assets/patient/home/content-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
}
.content_middle {
  width: 920px;
  height: 656px;
  position: relative;
  background-image: url('@/assets/patient/home/content-bg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.content_right {
  width: 463px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
  .content_right_top {
    width: 100%;
    height: 195px;
    position: relative;
    background-image: url('@/assets/patient/home/content-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
  .content_right_middle {
    width: 100%;
    height: 230px;
    position: relative;
    background-image: url('@/assets/patient/home/content-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
  .content_right_bottom {
    width: 100%;
    height: 380px;
    position: relative;
    background-image: url('@/assets/patient/home/content-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
}
.blue_block {
  width: 10px;
  height: 10px;
  background: #48a2fc;
  border-radius: 2px;
}
.patient-risk-title {
  width: 100%;
  height: 36px;
  position: relative;
  top: 0;
  left: 0;
  font-size: 20px;
  color: #fff;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 5px;
  padding-left: 10px;
  background-image: url('@/assets/patient/home/content-title-bg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.patient-alarm-title {
  width: 100%;
  height: 40px;
  position: relative;
  top: 0;
  left: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  background-image: url('@/assets/patient/home/alarm-timing.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.patient-alarm-title-right {
  width: 60%;
  height: 40px;
  font-size: 20px;
  color: #fff;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 5px;
}
.progress-title {
  width: 100%;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 5px;
}
.crown1 {
  width: 20px;
  height: 20px;
  background-image: url('@/assets/patient/home/crown1.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.crown2 {
  width: 20px;
  height: 20px;
  background-image: url('@/assets/patient/home/crown2.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.crown3 {
  width: 20px;
  height: 20px;
  background-image: url('@/assets/patient/home/crown3.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.progress-bar {
  width: 100%;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 5px;
}
.progress-fill {
  width: 85%;
  height: 100%;
}
.progress-item {
  width: 100%;
}
.progress-description {
  color: #fff;
  font-family: MiroSoft YaHei;
  font-size: 14px;
  font-weight: bold;
}
.progress-number {
  height: 20px;
  font-weight: 400;
  color: #ccc;
  font-family: MiroSoft YaHei;
  font-size: 14px;
  //文字居中
  line-height: 20px;
}
.cure-rate-list {
  width: 100%;
  height: 90%;
}
.alarm-content {
  width: 100%;
  height: 92%;
  display: flex;
  flex-direction: column;
  color: #fff;
  overflow-y: auto;
  //隐藏滚动条背景,但是能看到滚动条为灰色
  &::-webkit-scrollbar {
    width: 8px;
    background-color: transparent;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #7a797962;
    border-radius: 10px;
  }
  &::-webkit-scrollbar-track {
    background-color: transparent;
  }
  &::-webkit-scrollbar-corner {
    background-color: transparent;
  }
}
.alarm-item {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: space-between;
  color: #e6e3e3;
  font-family: MiroSoft YaHei;
  font-size: 14px;
  font-weight: bold;
  padding: 10px;
  margin-bottom: 10px;
}
.send-email {
  color: #ee0c0c;
  font-family: MiroSoft YaHei;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
}
.send-all-email {
  padding: 0 10px;
  height: 30px;
  line-height: 30px;
  color: #fff;
  font-family: MiroSoft YaHei;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
}
.cure-top3 {
  width: 100%;
  height: 140px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.cure-other {
  width: 100%;
  height: 180px;
  margin-top: 10px;
  padding-bottom: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: auto;
  //隐藏滚动条背景,但是能看到滚动条为灰色
  &::-webkit-scrollbar {
    width: 8px;
    background-color: transparent;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #7a797962;
    border-radius: 10px;
  }
  &::-webkit-scrollbar-track {
    background-color: transparent;
  }
  &::-webkit-scrollbar-corner {
    background-color: transparent;
  }
}
.cure-blue-bg {
  width: 100px;
  height: 93px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  gap: 5px;
  background-image: url('@/assets/patient/home/NO-other.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.cure-orange-bg {
  width: 100px;
  height: 93px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  gap: 5px;
  background-image: url('@/assets/patient/home/NO1.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.top-title {
  height: 25px;
  font-size: 20px;
  font-weight: bold;
  line-height: 25px;
}
.top1-icon {
  width: 30px;
  height: 30px;
  background-image: url('@/assets/patient/home/NO1-icon.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.top2-icon {
  width: 30px;
  height: 30px;
  background-image: url('@/assets/patient/home/NO2-icon.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.top3-icon {
  width: 30px;
  height: 30px;
  background-image: url('@/assets/patient/home/NO3-icon.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.top-content {
  width: 130px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding-top: 25px;
}
.hospital-name {
  width: 100%;
  font-size: 14px;
  font-weight: bold;
  color: #fff;
  text-align: center;
  //不换行
  white-space: nowrap;
  //超出部分用省略号表示
  overflow: hidden;
  text-overflow: ellipsis;
}
.cure-item0 {
  width: 410px;
  height: 45px;
  //不压缩
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  background: transparent;
}
.cure-item1 {
  width: 410px;
  height: 45px;
  //不压缩
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  background: #1d4183;
}
.horn {
  width: 20px;
  height: 20px;
  background-image: url('@/assets/patient/home/horn.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.other-content {
  width: 330px;
  height: 20px;
  font-size: 14px;
  font-weight: bold;
  color: #fff;
  font-family: MiroSoft YaHei;
  line-height: 20px;
}
.random-chart {
  width: 100%;
  height: 85%;
}
.hight-risk-chart {
  width: 100%;
  height: 83%;
}
.recent-three-add-chart {
  width: 100%;
  height: 91%;
}
.infinite-list {
  height: 330px;
  padding: 0 5px;
  margin: 0;
  list-style: none;
  //隐藏滚动条背景,但是能看到滚动条为灰色
  &::-webkit-scrollbar {
    width: 8px;
    background-color: transparent;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #7a797962;
    border-radius: 10px;
  }
  &::-webkit-scrollbar-track {
    background-color: transparent;
  }
  &::-webkit-scrollbar-corner {
    background-color: transparent;
  }
}
.alarm-content-item {
  width: 100%;
  display: flex;
  flex-shrink: 1;
  align-items: center;
  justify-content: center;
  background: transparent;
}
.infinite-list .infinite-list-item0 {
  width: 410px;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  background: transparent;
}
.infinite-list .infinite-list-item1 {
  width: 410px;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  background: #1d4183;
}
.infinite-list .infinite-list-item + .list-item {
}
.alarm-name {
  width: 100px;
  height: 50px;
  line-height: 50px;
  //不换行
  white-space: nowrap;
  //超出部分用省略号表示
  overflow: hidden;
  text-overflow: ellipsis;
}
.alarm-hospital {
  width: 220px;
  height: 50px;
  line-height: 50px;
  //不换行
  white-space: nowrap;
  //超出部分用省略号表示
  overflow: hidden;
  text-overflow: ellipsis;
}
.alarm-idcard {
  width: 220px;
  height: 50px;
  line-height: 50px;
  //不换行
  white-space: nowrap;
  //超出部分用省略号表示
  overflow: hidden;
  text-overflow: ellipsis;
}
.alarm-time {
  width: 190px;
  height: 50px;
  line-height: 50px;
  //不换行
  white-space: nowrap;
  //超出部分用省略号表示
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
